package com.manage.module.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.module.entity.User;
import com.manage.module.entity.UserLog;
import com.manage.module.mapper.UserMapper;
import com.manage.module.service.UserLogService;
import com.manage.module.service.UserService;
import com.manage.utils.CacheUtils;
import com.manage.utils.RequestSession;
import com.manage.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserLogService userLogService;
    @Override
    public User login(User user, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        User resultUser = baseMapper.login(user);
        if(resultUser != null) {
            // 将用户信息存入session并将sessionId放入Cookie中返回给浏览器
//            RequestSession<User> requestSession = new RequestSession<>();
//            httpServletResponse.addCookie(new Cookie("u_id",requestSession.getSessionId()));
//            requestSession.setUser(resultUser);
            // 用户登录成功添加登录日志
            UserLog userLog = UserLog.builder()
                    .userId(resultUser.getId())
                    .ip(RequestUtils.getRemoteIpByServletRequest(httpServletRequest))
                    .loginTime(LocalDateTime.now())
                    .build();
            userLogService.save(userLog);
            //缓存登录状态
            CacheUtils.saveCache(resultUser.getId(), true);
            log.info("用户登录成功 userid：", resultUser.getId());

            return resultUser;
        }
        log.error("用户不存在或密码错误 userid：", user.getUsername());
        throw new Exception("用户不存在或密码错误");
    }

    @Override
    public Page<Map<String,Object>> queryPageList(Page page, User user) {
        return baseMapper.queryPageList(page,user);
    }
}
